<?php
/**
 *
 *
 */
namespace Yz\modules\Generic;

use \Yz_Array;

class Controller_Edit
    extends \Yz_Controller_Templated
    implements \Yz_Controller_Interface
{

    /**
     *  forbid access beyond admin panel
     *
     */
    const ACCESS = \Yz_Access_Interface::AC_ADMIN;

    /**
     *  @var string|null null -> Default model
     */
    protected $modelName = null;

    /**
     *  @var string|null null -> Default model
     */
    protected $formName = null;


    protected $model;
    protected $item;
    protected $form;
    protected $error;

    /**
     *
     */
    protected function doGET()
    {
        debug();
        return $this
            ->loadModel()
            ->loadItem()
            ->createForm($assignValuesFromItem=true)
            ->output()
        ;
    }

    /**
     *
     */
    protected function doPOST()
    {

        debug();
        return $this
            ->loadModel()
            ->loadItem()
            ->createForm($assignValuesFromItem=false)
            ->parseForm()
            ->updateModel()
            ->output()
        ;
    }

    /**
     *
     */
    protected function loadModel()
    {
        debug();
        $this->model = $this->module->selectModel($this->modelName);
        return $this;
    }

    /**
     *
     */
    protected function loadItem()
    {
        debug();
        $id = $this->fetchArgument(0);
        $this->item = $this->model->fetchItemById($id);
        return $this;
    }

    /**
     *
     */
    protected function createForm($assignValuesFromItem)
    {
        debug();
        $formName = $this->formName;
        if (null === $formName) {
            $formName = 'Default';
        }
        $formClass = $this->module->resolveLibrary('Form', $formName);
        $form = new $formClass($this->item->describe());
        $form->setName("theForm");
        if ($assignValuesFromItem) {
            $form->assignValues($this->item);
        }
        $this->form = $form;
        return $this;
    }

    /**
     *
     */
    protected function parseForm()
    {

        debug();
        $form = $this->form;
        try {
            $form->parse($this->getRequest()->getPOSTArguments());
            $this->item->assignValues($form->getValues());
        } catch (\Yz_Form_Parser_Exception $ex) {
            $this->error = $ex;
            debug($ex->toString(true));
            // TODO add global error message
        }
        return $this;
    }

    /**
     *
     */
    protected function output()
    {
        $template = $this->selectTemplate();
        $template->item = $this->item;
        $template->includeForm($this->form);

        $template->errorMessage = $this->error;

        return $this->outputTemplate($template);
    }


    /**
     *
     */
    protected function updateModel()
    {
        if (null === $this->error) {
            try {
                $this->model->updateItemById($this->item);
            } catch (\Yz_SQL_Exception_Unique $ex) {
                $this->error = $ex;
            }
        }
        return $this;
    }

}
